﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyLib.Web.Pages {
	/// <summary>
	/// 
	/// </summary>
	public abstract class ErrorPage : MyPage {
		/// <summary>
		/// 
		/// </summary>
		private void InitButtons() {
			var button1 = this.FindDescendantControl<Control>("ErrorReload");
			var button2 = this.FindDescendantControl<Control>("ErrorGoHome");

			var reloadScript = @"
location.reload(true);return false;";
			var gohomeScript = string.Format(@"
location.replace('{0}');return false;", ResolveClientUrl("~/").Replace("'", "\\'"));

			if (button1 is LinkButton) {
				((LinkButton)button1).OnClientClick = reloadScript;
				((LinkButton)button1).Text = Properties.Main.Reload;
			} else if (button1 is Button) {
				((Button)button1).OnClientClick = reloadScript;
				((Button)button1).Text = Properties.Main.Reload;
			} else if (button1 is HtmlControl) {
				((HtmlControl)button1).Attributes["onclick"] = reloadScript;
				((HtmlControl)button1).Controls.Add(new Literal { Text = Properties.Main.Reload, Mode = LiteralMode.Encode });
			}

			if (button2 is LinkButton) {
				((LinkButton)button2).OnClientClick = gohomeScript;
				((LinkButton)button2).Text = Properties.Main.GoHome;
			} else if (button2 is Button) {
				((Button)button2).OnClientClick = gohomeScript;
				((Button)button2).Text = Properties.Main.GoHome;
			} else if (button2 is HtmlControl) {
				((HtmlControl)button2).Attributes["onclick"] = gohomeScript;
				((HtmlControl)button2).Controls.Add(new Literal { Text = Properties.Main.GoHome, Mode = LiteralMode.Encode });
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected virtual string GetErrorImagePath() {
			return "";
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			InitButtons();
		}
	} // end of ErrorPage.
}
